//
//  AccountInfoController.m
//  BCNADemo
//
//  Created by 马东朝 on 2018/5/16.
//  Copyright © 2018年 马东朝. All rights reserved.
//

#import "AccountInfoController.h"
#define CellInd @"cell"
@interface AccountInfoController ()<UITableViewDelegate, UITableViewDataSource>
{
    UITableView *tableView;
    
    NSArray *iconArray;
    NSDictionary *dataDic;
}
@end

@implementation AccountInfoController
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    NSMutableString *phoneStr = [NSMutableString stringWithCapacity:0];
    NSMutableString *emailStr = [NSMutableString stringWithCapacity:0];
    if ([[[NSUserDefaults standardUserDefaults] valueForKey:isLogInEmail] isEqualToString:[NSString enCryptKey:postKey strPassword:@"1"]]) {
        NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
        if ([ud objectForKey:PhoneNum]) {
            //key对应的value存在
            id value = [ud objectForKey:PhoneNum];
            phoneStr = [NSMutableString stringWithString:value];
        }else
        {
            phoneStr = [NSMutableString stringWithString:@""];
            //key对应的value不存在
        }
        
        emailStr = [NSMutableString stringWithString:[[NSUserDefaults standardUserDefaults] valueForKey:EmailAdd]];
    }else{
        NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
        if ([ud objectForKey:EmailAdd]) {
            //key对应的value存在
            id value = [ud objectForKey:EmailAdd];
            emailStr = [NSMutableString stringWithString:value];
        }else
        {
            emailStr = [NSMutableString stringWithString:@""];
            //key对应的value不存在
        }
        
        phoneStr = [NSMutableString stringWithString:[[NSUserDefaults standardUserDefaults] valueForKey:PhoneNum]];
    }
    NSLog(@"phoneStr==%@", phoneStr);
    dataDic = @{@"phone":phoneStr, @"email":emailStr, @"QQ":@"11111111111", @"wechat":@"2231231241", @"fcacebook":@"102y0y012370"};
    [tableView reloadData];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self addTableView];
    
}
- (void)addTableView{
    iconArray = @[@"phone@3x.png", @"email@3x.png", @"password@3x.png",@"QQq@3x.png", @"wechat1@3x.png", @"FB@3x.png"];
    tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.but.frame) + kHeight(90), ScreenWidth, ScreenHeight -CGRectGetMaxY(self.but.frame) - kHeight(90)) style:UITableViewStylePlain];
    tableView.scrollEnabled = NO;
    tableView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:tableView];
    tableView.delegate = self;
    tableView.dataSource = self;
    [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:CellInd];
    tableView.rowHeight = kHeight(180);
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
}
#pragma mark -- UITableViewDelegate, UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return section == 0 ? kHeight(130):0;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        UIView *aView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenHeight, kHeight(130))];
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(kWidth(120), 0, ScreenWidth/2, kHeight(70))];
        titleLabel.textColor = [UIColor colorWithHexString:@"#ffffff"];
        titleLabel.font = [UIFont systemFontOfSize:kHeight(60)];
        titleLabel.text = StringWithKey(@"Account6");
        [aView addSubview:titleLabel];
        return aView;
    }else{
        return nil;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return section != 0 ? kHeight(140) : kHeight(350);
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    CGFloat height = section != 0 ? kHeight(140) : kHeight(350);
    UIView *aView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, height)];
    if (section != 0) {
        UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(kWidth(70), kHeight(69), ScreenWidth-kWidth(70), kHeight(2))];
        [aView addSubview:lineView];
        lineView.backgroundColor = [UIColor colorWithHexString:@"#242535"];
    }else{
        UIButton *logOutBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [aView addSubview:logOutBtn];
        logOutBtn.frame =  CGRectMake(kWidth(90), kHeight(230), ScreenWidth-kWidth(180), kHeight(160));
        logOutBtn.titleLabel.textColor = [UIColor colorWithHexString:@"#40b5f8"];
        [logOutBtn setTitle:StringWithKey(@"Account7") forState:UIControlStateNormal];
        logOutBtn.layer.cornerRadius = kHeight(80);
        logOutBtn.layer.masksToBounds = YES;
        logOutBtn.layer.borderColor = [UIColor colorWithHexString:@"#40b5f8"].CGColor;
        logOutBtn.layer.borderWidth =  kHeight(2);
        [logOutBtn addTarget:self action:@selector(handleLogOut) forControlEvents:UIControlEventTouchUpInside];
    }
    
    return aView;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 3;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    UITableViewCell * cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellInd];
    if (indexPath.section == 0 && indexPath.row == 2) {
       cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    
    cell.backgroundColor = [UIColor clearColor];
    cell.imageView.image = [UIImage imageNamed:iconArray[indexPath.row + indexPath.section * 3]];
    NSString *strkey = [NSString stringWithFormat:@"Account%ld", indexPath.row + indexPath.section * 3];
    cell.textLabel.text = StringWithKey(strkey);
    cell.textLabel.font = [UIFont systemFontOfSize:kHeight(48)];
    cell.textLabel.textColor = [UIColor colorWithHexString:@"#ffffff"];
    cell.detailTextLabel.textColor = [UIColor colorWithHexString:@"#ffffff"];
    cell.detailTextLabel.font = [UIFont systemFontOfSize:kHeight(46)];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (indexPath.section == 0 ) {
        if (indexPath.row == 0) {
            NSString *phoneStr = dataDic[@"phone"];
            if (phoneStr.length > 0) {
                cell.detailTextLabel.text = dataDic[@"phone"];
            }else{
                NSString *reguStr = @"绑定 >";
                NSMutableAttributedString *AttributedStr = [[NSMutableAttributedString alloc]initWithString:reguStr];
                
                [AttributedStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:kHeight(36)] range:NSMakeRange(0, reguStr.length)];
                [AttributedStr addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHexString:@"#40b5f8"] range:NSMakeRange(0, reguStr.length)];
                cell.detailTextLabel.attributedText = AttributedStr;
            }
            
        }
        if (indexPath.row == 1) {
            NSString *phoneStr = dataDic[@"email"];
            if (phoneStr.length > 0) {
                cell.detailTextLabel.text = dataDic[@"email"];
            }else{
                NSString *reguStr = @"绑定 >";
                NSMutableAttributedString *AttributedStr = [[NSMutableAttributedString alloc]initWithString:reguStr];
                
                [AttributedStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:kHeight(36)] range:NSMakeRange(0, reguStr.length)];
                [AttributedStr addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHexString:@"#40b5f8"] range:NSMakeRange(0, reguStr.length)];
                cell.detailTextLabel.attributedText = AttributedStr;
            }
            
        }
        
    }else{
        switch (indexPath.row) {
            case 0:
            {
                
                cell.detailTextLabel.text = dataDic[@"QQ"];
                
            }
                break;
            case 1:
            {
                cell.detailTextLabel.text = dataDic[@"wechat"];
            }
                break;
            case 2:
            {
                cell.detailTextLabel.text = dataDic[@"fcacebook"];
            }
                break;
                
            default:
                break;
        }
    }
    
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    switch (indexPath.row) {
        case 0:
        {
            NSString *phoneStr = dataDic[@"phone"];
            if (phoneStr.length > 0) {
            }else{
            BindingController *bindVC = [[BindingController alloc]init];
            bindVC.loginStyle = @"1";
            [self.navigationController pushViewController:bindVC animated:YES];
            }
        }
            break;
        case 1:
        {
            NSString *phoneStr = dataDic[@"email"];
            if (phoneStr.length > 0) {
            }else{
            BindingController *bindVC = [[BindingController alloc]init];
            bindVC.loginStyle = @"0";
            [self.navigationController pushViewController:bindVC animated:YES];
            }
        }
            break;
        case 2:
        {
            [self.navigationController pushViewController:[[ResetPasswordController alloc]init] animated:YES];
        }
            break;
            
        default:
            break;
    }
    
}
- (void)handleLogOut{
    
    [self postSignup];
    //退出登录");
}
- (void)postSignup{
    NSLog(@"%@", [NSString deCryptKey:postKey strPassword:[[NSUserDefaults standardUserDefaults] valueForKey:PassWord]]);
    [GLHttpTool post:[NSString stringWithFormat:@"%@/user/logout", UrlAddress] params:@{@"unique_id":[NSString enCryptKey:postKey strPassword:[[NSUserDefaults standardUserDefaults] valueForKey:UniqueU_id]], @"password":[NSString enCryptKey:postKey strPassword:[[NSUserDefaults standardUserDefaults] valueForKey:PassWord]]} success:^(id responseObj) {
        NSLog(@"responseObj==%@", responseObj);
        NSString *code = [NSString stringWithFormat:@"%@", responseObj[@"code"]];
        NSString *msg = [NSString stringWithFormat:@"%@", responseObj[@"msg"]];
        if ([code isEqualToString:@"200"]) {
            
            [[NSUserDefaults standardUserDefaults] setValue:@"NO" forKey:ISLogin];
            [[NSUserDefaults standardUserDefaults] removeObjectForKey:PassWord];
            [[NSUserDefaults standardUserDefaults] removeObjectForKey:EmailAdd];
            [[NSUserDefaults standardUserDefaults] removeObjectForKey:PhoneNum];
            [UIApplication sharedApplication].keyWindow.rootViewController = [[UINavigationController alloc]initWithRootViewController:[[LRChoiceController alloc]init]];
            
        }else{
            
            AlterView(StringWithKey(@"ALter0"), msg, StringWithKey(@"ALter8"));
            
        }
    } failure:^(NSError *error) {
        NSLog(@"error==%@", error);
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
